package controller

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"golang.org/x/crypto/bcrypt"
	"gorm.io/gorm"
	"log"
	"wish_go/src/main/com/ajie/common"
	"wish_go/src/main/com/ajie/dao"
	"wish_go/src/main/com/ajie/model"
	"wish_go/src/main/com/ajie/result"
	"wish_go/src/main/com/ajie/vo"
)

// 登陆方法
func Login(req *gin.Context) {
	// 获取前端参数
	json := vo.LoginVo{}
	_ = req.BindJSON(&json)
	log.Printf("登陆参数：%v", json)
	// 校验验证码是否正确
	if !common.Instance().Verify(json.Uuid, json.Code) {
		result.Error(req, 500, "验证码错误")
		return
	}
	if len(json.Username) == 0 || len(json.Password) == 0 {
		result.Error(req, 500, "用户名密码不能为空")
		return
	}
	db := common.GetDB()
	user := getUserByName(db, json.Username)
	if user.Id == 0 {
		result.Error(req, 500, "账号不存在")
		return
	}
	if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(json.Password)); err != nil {
		result.Error(req, 500, "密码错误")
		return
	}
	token, err := common.GenerateToken(user)
	if err != nil {
		result.Error(req, 500, "系统异常")
		log.Printf("token生成失败: %v", err)
		return
	}
	result.SuccessData(req, "登陆成功", gin.H{"tokenHead": viper.GetString("jwt.tokenHead"), "token": token})
}

// 根据用户名查询用户信息
func getUserByName(db *gorm.DB, name string) model.User {
	var user model.User
	db.Where("name = ?", name).First(&user)
	return user
}

func GetInfo(ctx *gin.Context) {
	user, _ := ctx.Get("user")
	result.SuccessData(ctx, "用户信息", user)
}

func UserInfo(ctx *gin.Context) model.User {
	userInfo := model.User{}
	user, _ := ctx.Get("user")
	bytes, err := json.Marshal(user)
	if err != nil {
		return userInfo
	}
	_ = json.Unmarshal(bytes, &userInfo)
	return userInfo
}

func UserList(req *gin.Context) {
	userVo := vo.UserVo{}
	if err := req.BindJSON(&userVo); err != nil {
		result.Error(req, 500, "参数解析异常:"+err.Error())
		return
	}
	userVo.PageInfo = common.GeneratePageInfo(userVo.PageInfo)
	rows, total, err := dao.PageUser(&userVo)
	if err != nil {
		result.Error(req, 500, "查询异常:"+err.Error())
		return
	}
	result.PageRsult(req, rows, total)
}

func SetRole(req *gin.Context) {
	var user model.User
	if err := req.BindJSON(&user); err != nil {
		result.Error(req, 500, "参数解析异常:"+err.Error())
		return
	}
	bcryptPassword, gpErr := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
	if gpErr != nil {
		result.Error(req, 500, "加密失败:"+gpErr.Error())
		return
	}
	user.Password = string(bcryptPassword)
	i, dbErr := dao.SetRole(user)
	if dbErr != nil {
		result.Error(req, 500, "操作异常:"+dbErr.Error())
		return
	}
	if i < 1 {
		result.Error(req, 500, "修改失败")
		return
	}
	result.Success(req, "修改成功")
}

func ResetPassword(req *gin.Context) {
	var user model.User
	if err := req.BindJSON(&user); err != nil {
		result.Error(req, 500, "参数解析异常:"+err.Error())
		return
	}
	bcryptPassword, gpErr := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
	if gpErr != nil {
		result.Error(req, 500, "加密失败:"+gpErr.Error())
		return
	}
	user.Password = string(bcryptPassword)
	i, dbErr := dao.ResetPassword(user)
	if dbErr != nil {
		result.Error(req, 500, "操作异常:"+dbErr.Error())
		return
	}
	if i < 1 {
		result.Error(req, 500, "修改失败")
		return
	}
	result.Success(req, "修改成功")
}
